#!coding=utf8
from django.shortcuts import *
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from django.views.decorators.csrf import csrf_exempt
from django.http import HttpResponseRedirect
from util.form_util import *
from models import SuperAdmin
from enterprise_basic_information.models import User_admin,Enterprise,Enterprise_basic_information
import sys
reload(sys)
sys.setdefaultencoding('utf8')

@csrf_exempt
def login(request):
	if request.method == 'GET':
		return render_to_response('loginSuperAdmin.html')

	if request.method == 'POST':
		name = request.POST.get('name', None)
		pwd = request.POST.get('pwd', None)

		if not name or not pwd:
			params = {'error':u'为查找到账户!'}
			return render_to_response('loginSuperAdmin.html', params, context_instance=RequestContext(request))

		sas = SuperAdmin.objects.filter(user=name, pwd=pwd)

		if len(sas) == 0:
			params = {'error':u'用户名或密码错误！'}
			return render_to_response('loginSuperAdmin.html', params, context_instance=RequestContext(request))

		request.session['sa'] = sas[0]

		return HttpResponseRedirect('/sa/index/')

def index(request):
	sa = request.session['sa']

	if not sa:
		return HttpResponse(u'当前超级管理员用户错误')

	try:
		parks = User_admin.objects.filter(sa = sa.id)
		paginator = Paginator(parks, 10)
		page = request.GET.get('page')
		ps = paginator.page(page)
	except PageNotAnInteger:
		ps = paginator.page(1)
	except EmptyPage:
		ps = paginator.page(paginator.num_pages)
	except Exception as e:
		return HttpResponse(u"非法请求\n\n"+ e.message + e)

	return render_to_response('superadmin/park_list.html',{ 'ps':ps, 'paginator':paginator }, context_instance=RequestContext(request))


def parkDetail(request,id):
	sa = request.session['sa']

	if not sa:
		return HttpResponse(u'当前超级管理员用户错误')

	parkAdmin = User_admin.objects.get(sa = sa.id)

	return render_to_response('superadmin/park_info_detail.html', { 'u':parkAdmin, 'p': parkAdmin.getPark() } ,context_instance=RequestContext(request))

@csrf_exempt
def parkEditPwd(request,id):
	sa = request.session['sa']

	if not sa:
		return HttpResponse(u'当前超级管理员用户错误')

	if request.method == 'GET':
		parkAdmin = User_admin.objects.get(sa = sa.id)
		return render_to_response('superadmin/park_info_edit.html', { 'u':parkAdmin, 'p': parkAdmin.getPark() } ,context_instance=RequestContext(request))

	if request.method == 'POST':
		parkAdmin = User_admin.objects.get(sa = sa.id)

		user = request.POST.get('user', None)
		pwd = request.POST.get('pwd', None)

		parkAdmin.username = user
		parkAdmin.password = pwd
		parkAdmin.save()

		return HttpResponseRedirect('/sa/parkDetail/%s/' % id)

def parkEnterList(request, id):
	sa = request.session['sa']

	if not sa:
		return HttpResponse(u'当前超级管理员用户错误')

	ents = Enterprise.objects.filter(park_id=id)

	ents_basic_info = []
	ebis = Enterprise_basic_information.objects

	for e in ents:
		ents_basic_info.append(ebis.filter(enterprise_id=e.id).order_by('-id')[0])

	try:
		paginator = Paginator(ents_basic_info, 10)
		page = request.GET.get('page')
		ps = paginator.page(page)
	except PageNotAnInteger:
		ps = paginator.page(1)
	except EmptyPage:
		ps = paginator.page(paginator.num_pages)
	except Exception as e:
		return HttpResponse(u"非法请求\n\n"+ e.message + e)
	return render_to_response('superadmin/park_enter_list.html', {'ps':ents_basic_info, 'paginator':paginator}, context_instance=RequestContext(request))